package com.virgo.demo.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.virgo.common.entity.ListDataVo;
import com.virgo.common.utils.ICommonUtils;
import com.virgo.common.utils.ITimeUtils;
import com.virgo.demo.market.converter.AdvConverter;
import com.virgo.demo.market.converter.AdvShowConverter;
import com.virgo.demo.market.domain.AdvDo;
import com.virgo.demo.market.domain.bo.AdvBo;
import com.virgo.demo.market.domain.vo.AdvShowVo;
import com.virgo.demo.market.domain.vo.AdvVo;
import com.virgo.demo.market.mapper.AdvMapper;
import com.virgo.demo.market.service.AdvService;
import com.virgo.redis.annotation.ICache;
import com.virgo.web.config.SystemConfig;
import com.virgo.web.utils.IRequestUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.io.File;
import java.util.Arrays;
import java.util.List;

@Service
@RequiredArgsConstructor
public class AdvServiceImpl extends ServiceImpl<AdvMapper, AdvDo> implements AdvService {

	private final AdvMapper advMapper;

	private final AdvConverter advConverter;

	private final AdvShowConverter advShowConverter;

    private final SystemConfig systemConfig;

	/**
	 * 编辑广告
	 * @param advBo AdvBo
	 */
	@Override
	public void saveAdv(AdvBo advBo) {
		AdvDo advDo = new AdvDo();
		BeanUtils.copyProperties(advBo,advDo);
		advDo.setStartTime(ITimeUtils.gmStr2time(advBo.getStartTime()));
		advDo.setEndTime(ITimeUtils.gmStr2time(advBo.getEndTime()));
		if (ObjectUtils.isEmpty(advDo.getId()) || advDo.getId().compareTo(0L) < 1) {
			save(advDo);
			return;
		}
        if (ObjectUtils.isEmpty(advDo.getContent())) {
            advDo.setContent(null);
        }
		updateById(advDo);
	}

	/**
	 * 删除广告
	 * @param ids String
	 */
	@Override
	public void deleteAdv(String ids) {
		List<Long> idList = Arrays.asList(ICommonUtils.stringToArray(ids, ","));
        LambdaQueryWrapper<AdvDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(AdvDo::getContent);
        queryWrapper.in(AdvDo::getId,idList);
        List<AdvDo> advDos = advMapper.selectList(queryWrapper);
        List<String> contenList = advDos.stream().map(AdvDo::getContent).distinct().toList();
        for (String content : contenList) {
            if (!content.startsWith("http://") && !content.startsWith("https:://")) {
                File file = new File(systemConfig.getUploadPath() + content);
                file.delete();
            }
        }
        removeByIds(idList);
	}

	/**
	 * 广告列表
	 * @param request HttpServletRequest
	 * @return ListDataVo<AdvVo>
	 */
	@Override
	public ListDataVo<AdvVo> findList(HttpServletRequest request) {
		long currentPage = IRequestUtils.getLong(request,"page");
		long pageSize = IRequestUtils.getLong(request,"pageSize");
		currentPage = Math.max(currentPage,1);
		pageSize = Math.max(pageSize,10);

		LambdaQueryWrapper<AdvDo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.orderByDesc(AdvDo::getId);

		String name = IRequestUtils.getString(request,"name","");
		if (!ObjectUtils.isEmpty(name)) {
			queryWrapper.like(AdvDo::getName,name);
		}

		String positionCode = IRequestUtils.getString(request,"positionCode","");
		if (!ObjectUtils.isEmpty(positionCode)) {
			queryWrapper.like(AdvDo::getPositionCode,positionCode);
		}

		Page<AdvDo> page = new Page<>(currentPage, pageSize);
		IPage<AdvDo> iPage = advMapper.selectPage(page, queryWrapper);
		return new ListDataVo<>(iPage.getTotal(),iPage.getPages(),advConverter.convert(iPage.getRecords(), AdvVo.class));
	}

	/**
	 * 查询广告
	 * @param positionCode String
	 * @return List<AdvShowVo>
	 */
	@Override
	@ICache(keyPrefix = "adv", matchValue = "#positionCode", ttlMin = 300, ttlMax = 600)
	public List<AdvShowVo> advList(String positionCode) {
		Long nowTime = ITimeUtils.gmTime();
		LambdaQueryWrapper<AdvDo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(AdvDo::getPositionCode,positionCode);
		queryWrapper.le(AdvDo::getStartTime,nowTime);
		queryWrapper.gt(AdvDo::getEndTime,nowTime);
		return advShowConverter.convert(advMapper.selectList(queryWrapper),AdvShowVo.class);
	}
}